<template>
  <div class="login-container">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      class="login-form"
    >
      <img
        src="https://likede2-admin.itheima.net/img/logo.595745bd.png"
        alt=""
        class="loginLogo"
      >

      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="phone" />
        </span>
        <el-input
          v-model="form.loginName"
          maxlength="18"
          placeholder="请输入账号"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="lock" />
        </span>
        <el-input
          :key="passwordType"
          v-model="form.password"
          :type="passwordType"
          placeholder="请输入密码"
          maxlength="20"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <el-row :style="{ height: '47px' }" type="flex">
          <el-col
            :span="17"
          ><div class="grid-content bg-purple" />
            <span class="svg-container">
              <svg-icon icon-class="code" />
            </span>
            <el-input
              v-model="form.code"
              placeholder="请输入验证码"
              maxlength="4"
            />
          </el-col>
          <el-col :span="7">
            <img
              :src="'/likede/api/user-service/user/imageCode/'+form.clientToken"
              alt=""
              width="100%"
              height="100%"
              @click="setclientToken"
            ></el-col>
        </el-row>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        class="loginBtn"
        @click.native.prevent="handleLogin"
      >登录</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        loginName: 'demo',
        password: '888itcast.CN764%...',
        code: '',
        loginType: 0,
        clientToken: ''
      },
      big: [
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'I',
        'J',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'U',
        'V',
        'W',
        'X',
        'Y',
        'Z'
      ],
      small: [
        'a',
        'b',
        'C',
        'd',
        'e',
        'f',
        'g',
        'h',
        'i',
        'j',
        'k',
        'l',
        'm',
        'n',
        'o',
        'p',
        'q',
        'r',
        's',
        't',
        'u',
        'v',
        'w',
        'x',
        'y',
        'z'
      ],
      rules: {
        loginName: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password'
    }
  },
  async created() {
    this.setclientToken()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    setclientToken() {
      this.form.clientToken = ''
      let text
      for (let i = 0; i < 32; i++) {
        if (Math.floor(Math.random() * 3) === 1) {
          const a = Math.floor(Math.random() * 9)
          text = text + a
        } else if (Math.floor(Math.random() * 3) === 2) {
          const n = Math.floor(Math.random() * 26)
          const b = this.big[n]
          text = text + b
        } else {
          const n = Math.floor(Math.random() * 26)

          const c = this.small[n]
          text = text + c
        }
      }
      this.form.clientToken = text
    },
    async handleLogin() {
      try {
        await this.$refs.form.validate()
        await this.$store.dispatch('user/storeLogin', this.form)
        this.$router.push('/')
      } catch (error) {
        console.log('失败')
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

/* reset element-ui css */
.login-container {
  .el-input {
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999;
      height: 47px;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    border: 1px solid #e2e2e2;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  height: 100%;
  width: 100%;
  background-image: url(https://likede2-admin.itheima.net/img/background.be4fae7d.png);
  background-size: 100%;
  overflow: hidden;

  .login-form {
    position: absolute;
    width: 518px;
    height: 450px;
    top: 50%;
    left: 50%;
    margin-top: -225px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    box-shadow: 0 3px 70px 0 rgba(30, 111, 72, 0.35);
    border-radius: 10px;
    .loginBtn {
      width: 100%;
      height: 52px;
      background: linear-gradient(262deg, #2e50e1, #6878f0);
      opacity: 0.91;
      border-radius: 8px;
      color: #fff;
      text-shadow: 0 7px 22px #cfcfcf;
    }
    .loginLogo {
      position: absolute;
      width: 96px;
      height: 96px;
      top: -46px;
      left: 50%;
      margin-left: -48px;
    }
    .bottom {
      padding-top: 20px;
      line-height: 20px;
      font-size: 14px;
      color: #fd3333;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    color: #889aa4;
    font-size: 18px;
    padding: 6px 5px 6px 15px;
    color: inherit;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
